document.addEventListener('DOMContentLoaded', function () {
  // 点击注销按钮
  const logoutBtn = document.getElementById('logout-btn');
  if (logoutBtn) {  // 确保该元素存在
      logoutBtn.addEventListener('click', function () {
          console.log('退出登录按钮被点击了');  // 这个日志可以检查事件是否触发

          fetch('/logout', {
              method: 'POST',
              headers: {
                  'Content-Type': 'application/json'
              },
              body: JSON.stringify({})  // 如果有需要，添加请求体内容
          })
              .then(response => response.json())
              .then(data => {
                  if (data.success) {
                      window.location.href = data.redirect_url;  // 重定向到登录页面
                  } else {
                      console.error("退出登录失败，请重试。");
                  }
              })
              .catch(error => {
                  console.error('退出登录失败:', error);
              });
      });
  }

  // 注释掉flash消息处理部分
  /*
  document.addEventListener('DOMContentLoaded', function () {
      const flashMessages = document.querySelectorAll('.flash-message');
      flashMessages.forEach((flash) => {
          flash.classList.add('show'); // 显示消息
          setTimeout(() => {
              flash.classList.add('fade-out'); // 添加消失动画
          }, 3000); // 显示3秒后消失
          setTimeout(() => {
              flash.remove(); // 完全移除元素
          }, 3500); // 动画完成后移除
      });
  });
  */

  // 切换表单函数
  let changeForm = (e) => {
      const switchCtn = document.querySelector("#switch-cnt");
      const switchC1 = document.querySelector("#switch-c1");
      const switchC2 = document.querySelector("#switch-c2");
      const switchCircle = document.querySelectorAll(".switch_circle");
      const aContainer = document.querySelector("#a-container");
      const bContainer = document.querySelector("#b-container");

      if (!switchCtn || !switchC1 || !switchC2 || !switchCircle.length || !aContainer || !bContainer) {
          console.error("切换表单元素未正确加载");
          return;
      }

      // 切换动画
      switchCtn.classList.add("is-gx");
      setTimeout(function () {
          switchCtn.classList.remove("is-gx");
      }, 1500);

      // 切换元素的可见性和样式
      switchCtn.classList.toggle("is-txr");
      switchCircle[0].classList.toggle("is-txr");
      switchCircle[1].classList.toggle("is-txr");
      switchC1.classList.toggle("is-hidden");
      switchC2.classList.toggle("is-hidden");
      aContainer.classList.toggle("is-txl");
      bContainer.classList.toggle("is-txl");
      bContainer.classList.toggle("is-z");
  };

  // 获取切换按钮并绑定点击事件
  const switchBtn = document.querySelectorAll(".switch-btn");
  if (switchBtn.length) {
      switchBtn.forEach((btn) => btn.addEventListener("click", changeForm));
  } else {
      console.error("切换按钮未找到");
  }
});

function scrollToSection(sectionId) {
  document.querySelectorAll('.sidebar ul li a').forEach(function(link) {
    link.classList.remove('active');
  });
  document.querySelector(`.sidebar ul li a[href="#${sectionId}"]`).classList.add('active');

  const section = document.getElementById(sectionId);
  section.scrollIntoView({ behavior: 'smooth', block: 'start' });
}